package com.dimples.dd.system.controller;

import com.dimples.dd.common.result.CommonResult;
import com.dimples.dd.common.result.ResultCode;
import com.dimples.dd.common.util.servlet.ServletUtils;
import com.dimples.dd.security.annotation.SecurityWhitelistPaths;
import com.dimples.dd.system.framework.sms.core.enums.SmsChannelEnum;
import com.dimples.dd.system.service.SmsSendService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@Tag(name = "短信回调")
@RestController
@RequestMapping("/sms/callback")
@RequiredArgsConstructor
public class SysSmsCallbackController {

    private final SmsSendService smsSendService;

    @PostMapping("/aliyun")
    @SecurityWhitelistPaths
    @Operation(summary = "阿里云短信的回调", description = "参见 https://help.aliyun.com/zh/sms/developer-reference/configure-delivery-receipts-1 文档")
    public CommonResult<Boolean> receiveAliyunSmsStatus(HttpServletRequest request) throws Throwable {
        String text = ServletUtils.getBody(request);
        smsSendService.receiveSmsStatus(SmsChannelEnum.ALIYUN.getCode(), text);
        return CommonResult.success(ResultCode.SUCCESS_SMS_CALLBACK);
    }

    @PostMapping("/tencent")
    @SecurityWhitelistPaths
    @Operation(summary = "腾讯云短信的回调", description = "参见 https://cloud.tencent.com/document/product/382/59178 文档")
    public CommonResult<Boolean> receiveTencentSmsStatus(HttpServletRequest request) throws Throwable {
        String text = ServletUtils.getBody(request);
        smsSendService.receiveSmsStatus(SmsChannelEnum.TENCENT.getCode(), text);
        return CommonResult.success(ResultCode.SUCCESS_SMS_CALLBACK);
    }


    @PostMapping("/huawei")
    @SecurityWhitelistPaths
    @Operation(summary = "华为云短信的回调", description = "参见 https://support.huaweicloud.com/api-msgsms/sms_05_0003.html 文档")
    public CommonResult<Boolean> receiveHuaweiSmsStatus(HttpServletRequest request) throws Throwable {
        String text = ServletUtils.getBody(request);
        smsSendService.receiveSmsStatus(SmsChannelEnum.HUAWEI.getCode(), text);
        return CommonResult.success(ResultCode.SUCCESS_SMS_CALLBACK);
    }

}
